package com.baozun.houji.activity

import android.os.Bundle
import android.text.SpannableString
import android.text.Spanned
import android.text.TextPaint
import android.text.style.ClickableSpan
import android.view.Gravity
import android.view.View
import com.baozhun.mall.common.activity.WebContainerActivity
import com.baozhun.mall.common.base.BaseActivity
import com.baozhun.mall.common.base.BaseApplication
import com.baozhun.mall.common.util.CacheUtil
import com.baozhun.mall.common.util.Constants
import com.baozhun.mall.common.util.SchemeUtils
import com.baozhun.mall.common.util.StatusBarUtil
import com.baozhun.mall.common.widget.dialog.CommonDialog
import com.baozun.houji.R
import com.baozun.houji.databinding.ActivityWelcomeBinding
import com.baozun.houji.viewmodel.MainViewModel
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.StringUtils
import com.umeng.analytics.MobclickAgent
import com.zhpan.bannerview.BannerViewPager
import me.hgj.jetpackmvvm.ext.view.visible
import kotlin.system.exitProcess

/**
 * @Title：欢迎页activity
 * @Description：
 * @Author：xuefeng.zhu
 * @Since：2022/11/4
 * @Version：1.0.0
 */
class WelcomeActivity : BaseActivity<MainViewModel, ActivityWelcomeBinding>() {
    private val DELAY_TIME = 300L //延迟时间

    private lateinit var mViewPager: BannerViewPager<String>

    override fun initView(savedInstanceState: Bundle?) {
        StatusBarUtil.setTransparentForImageView(this, mDataBinding.welcomeImage)
        mViewPager = mDataBinding.bannerView as BannerViewPager<String>
        if (CacheUtil.isAgreePrivacyProtocol()) {
            mViewModel.getAppConfig {
                //不是第一次打开App 0.3秒后自动跳转到主页
                mDataBinding.welcomeImage.visible()
                mViewPager.postDelayed({
                    toMainPage()
                }, DELAY_TIME)
            }

        } else {
            showPrivacyDialog {
                mViewModel.getAppConfig {
                    toMainPage()
                }
            }
        }
    }

    override fun onClick(v: View?) {
        super.onClick(v)
        when (v?.id) {
        }
    }

    /**
     * 去主页
     */
    private fun toMainPage() {
        BaseApplication.mInstance.agreePrivacyProtocolInit()
        CacheUtil.setFirst(false)
        MainActivity.start(this)
        //处理Deeplink数据
        SchemeUtils.parseScheme(intent.data)
        finish()
    }

    /**
     * 显示协议dialog
     */
    private fun showPrivacyDialog(agreeCallBack: () -> Unit) {
        val content: String = getString(com.baozhun.mall.res.R.string.privacy_protocol_content)
        val args = "《隐私协议》"
        val args1 = "《用户服务协议》"
        //处理字符串样式
        val protocolContentSB: SpannableString = formatProtocolMessage(
            content, content.indexOf(args),
            args.length, content.indexOf(args1), args1.length
        )
        CommonDialog(this, {
            ActivityUtils.finishAllActivities()
            //注销友盟
            CacheUtil.setPrivacyProtocolStatus(false)
            exitProcess(0)
            MobclickAgent.onKillProcess(this)
        }, {
            CacheUtil.setPrivacyProtocolStatus(true)
            agreeCallBack.invoke()
        }).title(com.baozhun.mall.res.R.string.warm_reminder)
            .content(protocolContentSB)
            .contentGravity(Gravity.LEFT)
            .cancelText(com.baozhun.mall.res.R.string.exit_app)
            .confirmText(com.baozhun.mall.res.R.string.read_and_agree)
            .isClickableSpan(true)
            .dismissOnTouchOutside(false)
            .show()
    }

    /**
     * 格式化协议内容
     *
     * @param context
     * @param msg
     * @param start
     * @param length
     * @param start1
     * @param length1
     * @return
     */
    private fun formatProtocolMessage(msg: String, start: Int, length: Int, start1: Int, length1: Int): SpannableString {
        if (StringUtils.isEmpty(msg) || start >= msg.length) {
            return SpannableString(msg)
        }
        if (StringUtils.isEmpty(msg) || start1 >= msg.length) {
            return SpannableString(msg)
        }
        val spannableString = SpannableString(msg)
        var end = start + length
        if (end > msg.length) {
            end = msg.length - 1
        }
        var end1 = start1 + length1
        if (end1 > msg.length) {
            end1 = msg.length - 1
        }
        spannableString.setSpan(object : ClickableSpan() {
            override fun onClick(view: View) {
                //蓝色字体的点击事件
                WebContainerActivity.start(ActivityUtils.getTopActivity(), Constants.Url.PRIVACY_PROTOCOL, "隐私协议")
            }

            override fun updateDrawState(ds: TextPaint) {
                ds.color = BaseApplication.mInstance.getColor(com.baozhun.mall.res.R.color.color_EE612E)
                ds.clearShadowLayer()
                ds.isUnderlineText = false
            }
        }, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        spannableString.setSpan(object : ClickableSpan() {
            override fun onClick(view: View) {
                //蓝色字体的点击事件
                WebContainerActivity.start(ActivityUtils.getTopActivity(), Constants.Url.USER_PROTOCOL, "用户服务协议")
            }

            override fun updateDrawState(ds: TextPaint) {
                ds.color = BaseApplication.mInstance.getColor(com.baozhun.mall.res.R.color.color_EE612E)
                ds.clearShadowLayer()
                ds.isUnderlineText = false
            }
        }, start1, end1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        return spannableString
    }
}